package com.forum.controller;

import com.forum.common.Result;
import com.forum.dto.ChatRecordDto;
import com.forum.dto.ConversationDto;
import com.forum.dto.PrivateMessageRequest;
import com.forum.bean.Messages;
import com.forum.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService privateMessageService;

    /**
     * 请求示例：
     * {
     *   "currentUserId": 1,
     *   "targetUserId": 2
     * }
     */
    @PostMapping("/initiate")
    public Result<Messages> initiatePrivateMessage(@RequestBody PrivateMessageRequest req) {
        return privateMessageService.initiatePrivateMessage(req);
    }


    /**
     * 查询当前用户的私信会话列表，每个会话返回最新一条消息及对方用户信息，
     * 对方用户信息包含：id, name, avatar
     */
    @GetMapping("/conversations")
    public Result<List<ConversationDto>> getConversationList(@RequestParam("currentUserId") Integer currentUserId) {
        return privateMessageService.getConversationList(currentUserId);
    }


    /**
     * 删除当前用户和目标用户之间的所有私信记录
     * 返回删除的记录数提示
     */
    @DeleteMapping("/conversation")
    public Result<String> deleteConversation(
            @RequestParam("currentUserId") Integer currentUserId,
            @RequestParam("targetUserId") Integer targetUserId) {
        return privateMessageService.deleteConversation(currentUserId, targetUserId);
    }


    /**
     * GET /api/messages/chat?currentUserId=1&targetUserId=2
     * 查询当前用户与目标用户之间的所有聊天记录
     */
    @GetMapping("/chat")
    public Result<List<ChatRecordDto>> getChatRecords(@RequestParam("currentUserId") Integer currentUserId,
                                                      @RequestParam("targetUserId") Integer targetUserId) {
        return privateMessageService.getChatRecords(currentUserId, targetUserId);
    }



    @DeleteMapping("/delChat")
    public Result<String> delChatRecords(@RequestParam("currentUserId") Integer currentUserId,
                                                      @RequestParam("targetUserId") Integer targetUserId) {
        return privateMessageService.deleteConversation(currentUserId,targetUserId);
    }
}
